package com.eazy.lkv2.web.controller.system;

import com.eazy.lkv2.core.web.utils.Bundle;
import com.eazy.lkv2.web.entity.Role;
import com.eazy.lkv2.web.entity.User;
import com.eazy.lkv2.web.entity.UserRole;
import com.eazy.lkv2.web.service.system.SystemRoleService;
import com.eazy.lkv2.web.service.system.UserRoleService;
import com.eazy.lkv2.web.service.system.impl.UserRoleServiceImpl;
import com.eazy.lkv2.web.vo.RoleVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.eazy.lkv2.web.controller.BaseController;
import com.eazy.lkv2.web.service.system.SystemUserService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 系统用户管理
 */
@Controller  
@RequestMapping("/user") 
public class SystemUserController extends BaseController {

	@Autowired
	private SystemUserService systemUserService;

    @Autowired
    private SystemRoleService systemRoleService;

    @Autowired
    private UserRoleService userRoleService;
	
	 /**
     * 查询用户列表
     */
    @RequestMapping(value = "list" , method = RequestMethod.GET)  
    public ModelAndView selectUser(Model model) {  
    	model.addAttribute("data", systemUserService.queryByObject(null));
        return new ModelAndView("user/user_list");  
    }


    /**
     * 添加用户角色
     */
    @RequestMapping(value = "addUserRole" , method = { RequestMethod.POST, RequestMethod.GET })
    public String addUserRole(HttpServletRequest req) {
        String user_id = req.getParameter("user_id");
        String [] role_id = req.getParameterValues("role_id");
        UserRole userRole=new UserRole();
        userRole.setUserId(Integer.parseInt(user_id));
        userRoleService.remove(userRole);

        if(role_id!=null){
            for(int i =0 ; i< role_id.length; i++) {
                UserRole ur=new UserRole();
                ur.setUserId(Integer.parseInt(user_id));
                ur.setRoleId(Integer.parseInt(role_id[i]));
                userRoleService.create(ur);
            }
        }
        return "redirect:/user/list";
    }

    /**
     * 修改用户
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String updateUser(HttpServletRequest request) {
        Map<String, String> maps = getPageData();
        systemUserService.update(new User(maps.get("name"), maps.get("password")));
        return "redirect:/user/list";
    }


    /**
     * 删除用户
     */
    @RequiresPermissions("sys:user:delete")
    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    public String deleteUser(@PathVariable String id) {
        systemUserService.removeById(id);
        return "redirect:/user/list";
    }

    /**
     * 跳转到设置角色页面
     */
    @RequiresPermissions("sys:user:toRole")
    @RequestMapping(value = "toRole/{id}", method = RequestMethod.GET)
    public ModelAndView toRole(@PathVariable String id) {
        List<Map<String, Object>> self_role = systemRoleService.getUserRoles(id); // 用户拥有角色
        List<Role> all_role = systemRoleService.queryByObject(null); // 所有角色

        List<RoleVO>roleVOList=new ArrayList<>();
        for (Role role : all_role) {
            RoleVO roleVO=new RoleVO();
            roleVO.setRole(role);
            for (Map<String,Object> self : self_role) {
                if(Bundle.convStr(self.get("ID")).equals(roleVO.getId().toString())) {
                    roleVO.setPoint(1);
                }
            }
            roleVOList.add(roleVO);
        }

        ModelMap map = new ModelMap("all",roleVOList);
        map.put("id", id);
        return new ModelAndView("user/user_role",map);
    }


    /**
     * 跳转到修改用户页面并获取用户ID
     */
    @RequestMapping(value = "toUpdate", method = RequestMethod.GET)
    public ModelAndView toUpdateUser(@RequestParam String id) {
        ModelAndView modelAndView = new ModelAndView("/user/update");
        modelAndView.addObject("user", getCurrentUser());
        return modelAndView;
    }
    /**
     * 跳转到添加用户页面
     */
    @RequiresPermissions("sys:user:add")
    @RequestMapping(value = "toAdd", method = RequestMethod.GET)
    public ModelAndView toAddUser(HttpServletRequest request) {
        return new ModelAndView("user/user_add");
    }
    /**
     * 添加用户
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public String addUser() {
        Map<String, String> maps = getPageData();
        systemUserService.create(new User(maps.get("name"), maps.get("pwd"), maps.get("email")));
        return "redirect:/user/list";
    }
}
